package com.ebrothers.isns.map;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;

import com.ebrothers.isns.util.Utils;

public class BestLocator extends Locator {
	private OnLocatedListener mListener;

	public BestLocator(Context context) {
		super(context);
	}

	@Override
	public void locate() {
		if (Utils.isNetworkAvaliable(getContext())) {
			Criteria ct = new Criteria();
			ct.setAccuracy(Criteria.ACCURACY_FINE);
			ct.setAltitudeRequired(false);
			ct.setBearingRequired(false);
			ct.setCostAllowed(true);
			ct.setPowerRequirement(Criteria.POWER_LOW);
			requestLocationUpdates(getLocationManager().getBestProvider(ct,
					true));
		} else {
			if (mListener != null) {
				mListener.onLocateFaild();
			}
		}
	}

	@Override
	protected void onLocated(Location location) {
		removeUpdates();
		if (mListener != null) {
			mListener.onLocated(location);
		}
	}

	public void setOnLocatedListener(OnLocatedListener listener) {
		mListener = listener;
	}

	public interface OnLocatedListener {
		public void onLocated(Location location);

		public void onLocateFaild();
	}
}
